const koa = require("koa")
const app = new koa()
const Router = require("koa-router")
const router = new Router()
const axios = require("axios")
const cheerio = require("cheerio")
const url = "http://kuwo.cn/playlists"
router.get("/",async ctx=>{
    var html = await axios.get(url,{
        retryDelay:2000
    })
    const $ = cheerio.load(html.data,{
        decodeEntities:false
    })
    var result =[]
    var items = $(".rec_list .item")
    items.each((index,value)=>{
        var imgUrl = $(value).find(".pic_out img").attr("src")
        var title = $(value).find(".pic_out img").attr("alt")
        var playCount = $(value).find(".count").text()
        result.push({
            imgUrl,
            title,
            playCount
        })
    
    })
    ctx.body=result
})
router.get("/detail/:id",async ctx=>{
    let {id} = ctx.request.params
    var url = `http://kuwo.cn/playlist_detail/${id}`
    // console.log(url);
    var html = await axios.get(url)
    const $ = cheerio.load(html.data,{
        decodeEntities:false
    })
    let items = $(".content").find(".song_item")
    let arr = []
    items.each((index,value)=>{
        let name = $(value).find(".song_name a").html()
        let singer = $(value).find(".song_artist span").html()
        let time = $(value).find(".song_time span").html()
        arr.push({
            name,
            singer,
            time
        })
    })
    ctx.body={
        code:200,
        items:arr
    }
})
app.use(router.routes())
app.listen(8080)